package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-pro/cache"
	"order-pro/models"
	"strconv"
	"time"
)

func GoodAdd(c *gin.Context) {

	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))
	good_num, _ := strconv.Atoi(c.PostForm("good_num"))

	data := models.Good{

		GoodName:  good_name,
		GoodPrice: good_price,
		GoodNum:   good_num,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": data,
	})
	return

}

func GoodOrderAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_order_name := c.PostForm("good_order_name")
	good_order_tel := c.PostForm("good_order_tel")
	gid, _ := strconv.Atoi(c.PostForm("gid"))

	data := models.GoodOrder{

		OrderCode:     good_code,
		GoodOrderName: good_order_name,
		GoodOrderTel:  good_order_tel,
		Gid:           gid,
	}

	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func GoodList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var goodList []models.GoodOrder
	redis_key := "list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Offset(offset).Limit(size).Find(&goodList)
		jsonData, _ := json.Marshal(goodList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodList)
	}

	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  goodList,
		"total": total,
	})
	return
}

/*
SELECT
  good_orders.id AS OrderId,
  good_orders.good_order_name AS GoodName,
  good_orders.order_code AS OrderCode,
  good_orders.good_order_tel AS GoodOrderTel,
  goods.good_name AS GoodName,
  goods.good_price AS GoodPrice,
  goods.good_num AS GoodNum
FROM
  good_orders
  LEFT JOIN goods ON goods.id = good_orders.gid
*/

func GoodOrderList(c *gin.Context) {

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var goodList []models.GoodOrderList
	redis_key := "list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select("good_orders.id AS OrderId",
				" good_orders.good_order_name AS GoodOrderName",
				"good_orders.order_code AS OrderCode",
				"good_orders.good_order_tel AS GoodOrderTel",
				"goods.good_name AS GoodName",
				"goods.good_price AS GoodPrice",
				"goods.good_num AS GoodNum",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Offset(offset).Limit(size).Find(&goodList)
		jsonData, _ := json.Marshal(goodList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodList)
	}

	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  goodList,
		"total": total,
	})
	return
}
